using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace TestMonkey.Models
{
	/// <summary>
	/// 
	/// </summary>
    public class PropertyModel
    {
        readonly PropertyInfo m_Property;
        public PropertyModel(Setup setup, PropertyInfo property)
        {
            m_Setup = setup;
            m_Property = property;
        }

        public CodeString Name
        {
            get { return m_Property.Name; }
        }

        public bool IsIndexer
        {
            get { return m_Property.GetIndexParameters().Count() > 0; }
        }

        bool HasAttribute(string attributeName)
        {
            if(!attributeName.EndsWith("Attribute"))
                attributeName = attributeName + "Attribute";
            
            var attributes = m_Property.GetCustomAttributesData().Select(a => a.Constructor.DeclaringType.Name).ToList();
            return attributes.Any(name => name == attributeName);
        }

        public bool IsObsolete
        {
            get { return HasAttribute("Obsolete"); }
        }

        public bool CanRead
        {
            get { return m_Property.CanActuallyRead(); }
        }

        public bool CanWrite
        {
            get { return m_Property.CanActuallyWrite(); }
        }

        ClassModel m_PropertyType;
        readonly Setup m_Setup;
        public ClassModel Type
        {
            get {
                if (m_PropertyType == null)
                    m_PropertyType = new ClassModel(m_Setup, m_Property.PropertyType);

                return m_PropertyType;
            }
        }

        public bool IsNullable
        {
            get { return m_PropertyType.IsNullable; }
        }

    }
}
